<template>
  <div class="app-container  bg-gray">
    <el-row class="box-1">
      <el-col :span="24">
        <el-card>
          <div class="header">
            <div>
              <el-select v-model="searchTerms.company_id" placeholder="部门" clearable @change="getData">
                <el-option v-for="item in companyList" :key="item.id" :label="item.name" :value="item.id" />
              </el-select>
              <el-input v-model="searchTerms.searchValue" placeholder="请输入内容" class="search-input" @change="getData" />
              <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
            </div>
            <div>
              <el-button v-if="queryMatch(11)" type="success" class="bg-green" icon="el-icon-circle-plus" @click="toModify('新增服务人员')">新增服务人员</el-button>
              <el-button type="primary" class="bg-green" icon="el-icon-download" @click="toExportData">导出数据</el-button>
            </div>
          </div>
          <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
            <el-table-column prop="id" label="ID" width="65" header-align="center" align="center" sortable />
            <el-table-column prop="name" label="用户名" min-width="90" header-align="center" align="center" />
            <el-table-column prop="sex" label="性别" min-width="80" header-align="center" align="center" sortable>
              <template slot-scope="scope">
                {{ scope.row.sex?scope.row.sex:'--' }}
              </template>
            </el-table-column>
            <el-table-column prop="mobile" label="手机" min-width="120" header-align="center" align="center" >
              <template slot-scope="scope">
                {{ scope.row.mobile?scope.row.mobile:'--' }}
              </template>
            </el-table-column>
            <el-table-column prop="email" label="邮箱" min-width="170" header-align="center" align="center" />
            <el-table-column prop="hiredate" label="入职时间" min-width="110" header-align="center" align="center" sortable >
              <template slot-scope="scope">
                {{ parseTime(scope.row.hiredate, '{y}-{m}-{d}') }}
              </template>
            </el-table-column>
            <el-table-column prop="work_status" label="状态" width="160" header-align="center" align="center" fixed="right">
              <template slot-scope="scope">
                <el-tag v-if="scope.row['work_status']===1" type="success">接待</el-tag>
                <el-tag v-else type="info">休息</el-tag>
              </template>
            </el-table-column>
            <!--<el-table-column label="操作" width="180" header-align="center" align="center" fixed="right">
              <template slot-scope="scope">
                <el-button v-if="queryMatch(12)" type="primary" size="small" icon="el-icon-search" circle @click="toInfo(scope.row)"/>
                <el-button v-if="queryMatch(12)" type="success" size="small" icon="el-icon-edit" circle class="bg-green" @click="toModify('编辑信息', scope.row)" />
                <el-button v-if="queryMatch(13)" type="danger" size="small" icon="el-icon-delete" circle @click="toDelete(scope.row)" />
              </template>
            </el-table-column>-->
          </el-table>
          <el-pagination
            :current-page.sync="currentPage"
            :page-size="pageSize"
            :page-sizes="pageSizes"
            :total="total"
            class="pagination"
            layout="total, sizes, prev, pager, next, jumper"
            @current-change="handleCurrentChange"
            @size-change="handleSizeChange"/>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table'

export default {
  data() {
    return {
      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 280,
      searchTerms: {
        company_id: '',
        status: '',
        searchValue: ''
      },

      // 下拉框相关数据
      companyList: [],

      // 表格相关参数
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  created() {
    this.getData();
    this.getCompanyList();
  },

  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'work/staff'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue,
        company_id: this.searchTerms.company_id,
        branch_id: this.searchTerms.team,
        status: this.searchTerms.status
      }
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     *  获取公司列表
     *  接口:'company/all-list'
     */
    getCompanyList() {
      const url = 'company/all-list'
      this.$store.dispatch('GetConnect', { url }).then(res => {
        this.companyList = res.data
      }).catch(e => {
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    /**
     * 跳转用户详情
     * title: 新增/修改
     * row: 当前点击的行的数据,为空时为新增
     */
    toInfo(row) {
      this.$router.push({
        path: '/workOrder/staffInfo',
        query: { id: row.id }
      })
    },

    /**
     * 跳转用户修改页面
     * title: 新增/修改
     * row: 当前点击的行的数据,为空时为新增
     */
    toModify(title, row) {
      const query = { title: title }
      row && (query.id = row.id);
      this.$router.push({
        path: '/superWorkOrder/staffModify',
        query: query
      })
    },

    /**
     * 删除用户
     * row: 当前点击的行的数据user/delete
     */
    toDelete(row) {
      this.$confirm('此操作将删除该服务人员, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        const url = 'user/delete/' + row.id
        this.$store.dispatch('GetConnect', { url }).then(res => {
          this.$message.success('删除成功!')
          this.getData()
        }).catch(e => {
          this.$message.error(e.msg + '，请刷新或联系管理员')
        })
      })
    },

    // 导出数据
    toExportData() {
      this.downloadLoading = true;
      const url = this.$store.getters.serverUrl + '/admin/work/userToExcel?token=' + this.$store.getters.token + '&mallId=' + this.$store.getters.SaaSId;
      window.open(url);
      this.downloadLoading = false;
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../../styles/index.scss';
.box-1 {
  .header {
    @include flex-row(center, space-between);
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
    }
    .el-select {
      padding-right: 15px;
      width: 180px;
    }
    .search-input {
      width: 240px;
    }
    .do-btn {
      @extend .bg-green;
      margin-left: 6px;
    }
  }
}
</style>
